#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright © 2018 crane <crane@his-pc>
#
# Distributed under terms of the MIT license.

"""

"""


import math


class Solution:
    def binaryRepresentation(self, str_n):
        # 判断一个小数能否用二进制表示.
        str_int, str_float = str_n.split('.')

        int_rep = self.int_representation(str_int)
        can, float_rep = self.float_representation('0.' + str_float)

        if not can:
            return "ERROR"

        # if int_rep:
        #     print('int')
        if float_rep:
            return int_rep + '.' + float_rep

        if int_rep:
            return int_rep
        else:
            return ''

        # else:
        #     print('float')
        #     return float_rep

    def int_representation(self, int_part):
        if int_part == '':
            int_part = '0'
        num = int(int_part)

        return bin(num)[2:]

    def my_int_representation(self, int_part):
        # 使用类似于2倍增的方式来做
        if int_part == '':
            int_part = '0'
        int_part = int(int_part)

        if int_part == 0:
            return '0'

        max_digit = int(math.log(int_part, 2))
        result = ['0'] * (max_digit + 1)
        result[max_digit] = '1'

        remain = int_part - 2 ** max_digit

        while remain > 0:
            digit = int( math.log(remain, 2) )
            result[digit] = '1'
            remain -= 2 ** digit

        return "".join(result)

    # def float_representation(self, float_part):
    #     # 无法处理'065' 前面带0的数. 因为转换为整数时, 会丢失这个数字
    #     # 因为float计算可能会有误差, 所以这里使用int表示float
    #     # 例如: '0.123' 则使用 123表示
    #     ret = []

    #     float_part = float_part.rstrip('0')     # 首先去掉末尾的0
    #     if float_part == '':
    #         return True, ''

    #     float_part = int(float_part)
    #     cnt = 32

    #     while float_part != 0 and cnt > 0:
    #         cnt -= 1
    #         l = self.int_len(float_part)

    #         double = float_part * 2
    #         power = (10 ** l)
    #         carry  = double // power
    #         remain = double % power
    #         float_part = remain

    #         ret.append(str(carry))

    #     if float_part == 0:
    #         return True, ''.join(ret)
    #     else:
    #         return False, ''

    def float_representation(self, float_part):
        # 因为float计算可能会有误差, 所以这里使用int表示float
        # 例如: '0.123' 则使用 123表示
        ret = []
        # print(float_part)

        float_part = float_part.rstrip('0')     # 首先去掉末尾的0
        if float_part == '0.':
            return True, ''

        float_part = float(float_part)
        cnt = 32

        while (float_part - 0) > 0.0000000001 and cnt > 0:
            cnt -= 1
            # l = self.int_len(float_part)

            double = float_part * 2
            # power = (10 ** l)
            carry  = int(double - 0)
            # print(double)
            remain = double - carry
            float_part = remain

            ret.append(str(carry))

        if float_part < 0.0000000001:
            return True, ''.join(ret)
        else:
            # print(ret)
            return False, ''

    def int_len(self, num):
        if num == 0:
            return 1

        l = 0
        while num > 0:
            l += 1
            num = num // 10
        return l


# class Solution:
#     def can_binaryRepresentation(self, str_n):
#         # 判断一个小数能否用二进制表示.
#         str_float = str_n.split('.')[-1]
#         str_float = str_float.rstrip('0')       # 去掉末尾的0

#         l = len(str_float)
#         # fraction = [int(str_float), 10 ** l]
#         # fraction = [int(str_float), 10 ** l]
#         denominator = 10 ** l                   # 分母
#         numerator = int(str_float)              # 分子

#         gcd = math.gcd(denominator, numerator)
#         denominator = denominator / gcd

#         if denominator > 2 ** 32:
#             return False

#         return self.is_power_of_2(denominator)

#     def is_power_of_2(self, n):
#         if n == 2:
#             return True

#         elif n > 2:
#             if n % 2 == 1:
#                 return False
#             else:
#                 return self.is_power_of_2(n // 2)
#         else:
#             return False


def main():
    print("start main")
    # s = Solution()
    # ret = s.binaryRepresentation('0.126')
    # print(ret)
    s = Solution()
    ret = s.my_int_representation('31')
    print(ret)
    # ret = s.int_len(0)
    # print(ret)

    # can, ret = s.float_representation('0')
    # print(can, ret)

    # ret = s.binaryRepresentation('0.5')
    # print(ret)

if __name__ == "__main__":
    main()
